package com.abocode.aichat.server.modules.handler;

import com.abocode.aichat.data.*;
import com.abocode.aichat.server.context.channel.ChannelWrapper;
import com.abocode.aichat.server.context.ServerRuntimeContext;
import com.abocode.aichat.server.modules.ModuleServiceHandler;
import com.abocode.aichat.server.context.delivery.reply.Replies;
import com.alibaba.fastjson.JSON;

/**
 * @author guanxf
 */
public class PresenceGetServiceHandler implements ModuleServiceHandler {
    @Override
    public Replies execute(ChannelWrapper channelWrapper, MessageStanza stanza, ServerRuntimeContext serverRuntimeContext) {
        ReqMessageBody body = JSON.parseObject(stanza.body().toString(),ReqMessageBody.class);
        String jid = body.getJid();
        Entity entity = EntityImpl.parse(jid);
        boolean flag =serverRuntimeContext.getProtocolChannelRegistry().isOnline(entity);
        Replies replies = null;
        if (flag) {
            replies = new Replies();
            PresenceMessageBody map = new PresenceMessageBody(entity.getFullName(),entity.getPresence());
            replies.addReply(channelWrapper.getChannel(), StanzaWrapper.wrapper(stanza, map));
        }
        return replies;
    }

    @Override
    public ModuleServiceType moduleServiceType() {
        return ModuleServiceType.PresenceGet;
    }
}
